//
//  HCAppointView.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/1/21.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCAppointView.h"

#import "HCAppointCollectionViewCell.h"
#import "HCBottomTableViewCell.h"

@interface HCAppointView () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource> {
    UICollectionView *topCollectionView;
    BOOL first;
    NSIndexPath *indexPaths;
    UIScrollView *scrollView_;
    NSInteger currentindex;
    UITableView *tableView_;
    NSMutableArray *tableViews_arr;
    
    NSMutableArray *arr;
    NSArray *arrd;
    MJRefreshHeader *mj_header;
}

@end

@implementation HCAppointView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initializeSubView:frame];
    }
    return self;
}

-(void)initializeSubView:(CGRect)frame {
    first = YES;
    self.backgroundColor = YBKRGBCOLOR(242, 242, 242, 1);
    
    arr = [NSMutableArray array];
    
    arrd = [PublicFunction getFurtureDays:5];
    
    for (int i = 0; i<arrd.count; i++) {
        
        NSString *dateStr = [PublicFunction getDateFromNSString:arrd[i]];
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        
        NSString *mouth = [dateStr substringWithRange:NSMakeRange(5, 2)];
        
        NSString *day = [dateStr substringWithRange:NSMakeRange(8, 2)];
        
        [dic setObject:mouth forKey:@"mouth"];
        [dic setObject:day forKey:@"day"];
        
        [arr addObject:dic];
        
    }
    
    NSLog(@"当前时间:%@",    arr
);
    
    indexPaths = [NSIndexPath indexPathForRow:0 inSection:0];
    tableViews_arr = [NSMutableArray array];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    
    topCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, YBKAdaptiveH(82)+YBKAdaptiveH(10)) collectionViewLayout:flow];
    topCollectionView.delegate = self;
    topCollectionView.dataSource = self;
    topCollectionView.backgroundColor = [UIColor whiteColor];
    topCollectionView.scrollEnabled = NO;
    [self addSubview:topCollectionView];
    
    [topCollectionView registerNib:[UINib nibWithNibName:@"HCAppointCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"appointcollection"];
    
    //添加底部内容View
    scrollView_ = [[UIScrollView alloc] initWithFrame:CGRectMake(0, topCollectionView.frame.origin.y+topCollectionView.frame.size.height+YBKAdaptiveH(10), frame.size.width, frame.size.height - topCollectionView.frame.size.height-YBKAdaptiveH(10))];
    scrollView_.delegate = self;
    scrollView_.pagingEnabled= YES;
    [self addSubview:scrollView_];
    
    scrollView_.contentSize = CGSizeMake(frame.size.width*5, scrollView_.frame.size.height);
    __block __weak HCAppointView *weakself = self;
    for (NSInteger i = 0; i<5; i++) {
        tableView_ = [[UITableView alloc] initWithFrame:CGRectMake(scrollView_.frame.size.width*i, 0, scrollView_.frame.size.width, scrollView_.frame.size.height)];
        tableView_.tag = i;
        tableView_.delegate = self;
        tableView_.dataSource = self;
        tableView_.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView_.rowHeight = 90;
        [scrollView_ addSubview:tableView_];
        
        [tableView_ registerNib:[UINib nibWithNibName:@"HCBottomTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cellhomebottom"];
        
        [tableViews_arr addObject:tableView_];
        
        tableView_.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            NSLog(@"开始刷新");
            [weakself.delegate appointmentViewEndRefresh:tableViews_arr];
            
        }];
    }
    
}





-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    HCAppointCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"appointcollection" forIndexPath:indexPath];
    cell.cellIndexPath = indexPath;
    [cell cellWithModel:indexPaths];
    
    [cell cellAddData:arr[indexPath.row]];
    
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(YBKAdaptiveH(10), 10, 0, 10);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((self.frame.size.width - YBKAdaptiveW(60))/5.0, YBKAdaptiveH(83));
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    NSLog(@"点击的位置:%ld--%ld",indexPath.row,indexPath.section);
    
    indexPaths = indexPath;
    [topCollectionView reloadData];
    
    [scrollView_ setContentOffset:CGPointMake(self.frame.size.width*indexPath.row, 0) animated:YES];
}

#pragma mark tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_model) {
        return _model.index.count;
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HCBottomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellhomebottom" forIndexPath:indexPath];
    HCAppointmentData *model = _model.index[indexPath.row];
    if (_model) {
        [cell cellModel:model];
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([self.delegate respondsToSelector:@selector(appointIndexClicked:andDate:)]&&self.delegate) {
        [self.delegate appointIndexClicked:indexPath andDate:[PublicFunction getDateFromNSString:arrd[indexPaths.row]]];
    }
    
}

#pragma mark scrollView
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.x == self.frame.size.width) {
        indexPaths = [NSIndexPath indexPathForRow:1 inSection:0];

    }else if (scrollView.contentOffset.x == self.frame.size.width*2.0) {
        indexPaths = [NSIndexPath indexPathForRow:2 inSection:0];

    }else if (scrollView.contentOffset.x == self.frame.size.width*3.0) {
        indexPaths = [NSIndexPath indexPathForRow:3 inSection:0];

    }else if (scrollView.contentOffset.x == self.frame.size.width * 4.0) {
        indexPaths = [NSIndexPath indexPathForRow:4 inSection:0];

    }else{
        indexPaths = [NSIndexPath indexPathForRow:0 inSection:0];
    }
    [topCollectionView reloadData];
}

#pragma mark 写方法

-(void)setModel:(HCAppointmentModel *)model {
    _model = model;
    for (int i = 0; i<tableViews_arr.count; i++) {
        UITableView *tableView = tableViews_arr[i];
        [tableView reloadData];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
